package com.wsc.wsckeeper.activities;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;

import com.wsc.wsckeeper.R;
import com.wsc.wsckeeper.activities.base.BaseActivity;
import com.wsc.wsckeeper.enums.Mode;
import com.wsc.wsckeeper.listeners.TextWatcherImpl;
import com.wsc.wsckeeper.managers.ResourceManager;
import com.wsc.wsckeeper.managers.ScreenManager;
import com.wsc.wsckeeper.model.KModel;
import com.wsc.wsckeeper.model.KRuntimeModel;
import com.wsc.wsckeeper.model.entity.AccountE;
import com.wsc.wsckeeper.persistence.dao.AccountDAO;

public class AccountActivity extends BaseActivity {
	private static final int REQUEST_CODE = 1000;

	private EditText etDescription;
	private EditText etLogin;
	private EditText etPassword;
	private ImageButton ibGenerator;
	private Button btAction;

	private AccountE account;

	private List<EditText> components = new ArrayList<EditText>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_account);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void initComponents() {
		etDescription = (EditText) findViewById(R.id.account_destination);
		etLogin = (EditText) findViewById(R.id.account_login);
		etPassword = (EditText) findViewById(R.id.account_password);

		ibGenerator = (ImageButton) findViewById(R.id.account_generotor);
		btAction = (Button) findViewById(R.id.action);
	}

	@Override
	public void initValues() {
		components.add(etDescription);
		components.add(etLogin);
		components.add(etPassword);

		mode = KModel.getInstance().getAccountMode();
		account = KModel.getInstance().getAccount();

		if (account != null) {
			etDescription.setText(account.getDescription());
			etLogin.setText(account.getLogin());
			etPassword.setText(account.getPassword());
		}

		if (isMode(Mode.SHOW)) {
			editMode(false);
		} else {
			if (isMode(Mode.ADD)) {
				btAction.setText(R.string.action_add);
				getActionBar().setSubtitle(ResourceManager.getStringValue(R.string.action_add_account));
			} else {
				if (isMode(Mode.EDIT)) {
					btAction.setText(R.string.action_save);
					getActionBar().setSubtitle(ResourceManager.getStringValue(R.string.action_edit_account));
				}
			}
		}

		btAction.setEnabled(false);
	}

	@Override
	public void initListeners() {
		for (int i = 0; i < components.size(); i++) {
			final EditText edit = components.get(i);
			edit.addTextChangedListener(new TextWatcherImpl() {
				
				@Override
				public void afterTextChanged(Editable s) {
					btAction.setEnabled(true);
					edit.setError(null);
				}
			});
		}

		ibGenerator.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ScreenManager.getInstance().forwardForResult(AccountActivity.this, PasswordGeneratorActivity.class, REQUEST_CODE);
			}
		});

		btAction.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				try {
					if (isValidFields() == false)
						return;

					if (account == null) {
						account = new AccountE();
						account.setCreated(Calendar.getInstance().getTime());
					}

					account.setDescription(etDescription.getText().toString());
					account.setLogin(etLogin.getText().toString());
					account.setPassword(etPassword.getText().toString());

					if (mode == Mode.ADD) {
						AccountDAO.create(account);
						KRuntimeModel.getInstance().getAccounts().add(account);
					} else {
						AccountDAO.update(account);
					}
					onBackPressed();
				} catch (SQLException e) {
					showExceptionAsDialog(e);
				} catch (Exception e) {
					showExceptionAsDialog(e);
				}
			}
		});
	}

	private boolean isValidFields() {
		boolean isValid = true;
		if (etDescription.getText().toString().length() == 0) {
			etDescription.setError(ResourceManager.getStringValue(R.string.alert_account_destination));
			isValid = false;
		}
		if (etLogin.getText().toString().length() == 0) {
			etLogin.setError(ResourceManager.getStringValue(R.string.alert_account_login));
			isValid = false;
		}
//		if (etPassword.getText().toString().length() == 0) {
//			etPassword.setError(ResourceManager.getStringValue(R.string.alert_account_password));
//			isValid = false;
//		}

		return isValid;
	}

	private void editMode(boolean isEditMode) {
		if (!isEditMode) {
			btAction.setVisibility(View.INVISIBLE);
			ibGenerator.setVisibility(View.GONE);

			etDescription.setEnabled(isEditMode);
			etLogin.setEnabled(isEditMode);
			etPassword.setEnabled(isEditMode);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (data == null) {
			return;
		}

		String result = data.getExtras().getString(PasswordGeneratorActivity.RESULT);
		etPassword.setText(result);
		btAction.setEnabled(true);
	}

	@Override
	public void onBackPressed() {
		KModel.getInstance().setAccount(null);
		KModel.getInstance().setAccountMode(null);
		super.onBackPressed();
	}

}
